package com.sonic.ws.services.instrument;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceContext;

import com.sonic.db.dao.MQSecurityDAO;
import com.sonic.dto.ws.services.instrument.details.MQSecurityDetailsRequest;
import com.sonic.dto.ws.services.instrument.details.MQSecurityDetailsResponse;
import com.sonic.dto.ws.services.instrument.transactions.MQSecurityTransactionsRequest;
import com.sonic.dto.ws.services.instrument.transactions.MQSecurityTransactionsResponse;
import com.sonic.log.ejb.impl.MQLogger;
import com.sonic.ws.interfaces.instrument.IMQSecurityService;

@WebService(name = "SecurityService", serviceName = "SecurityService", portName = "SecurityServicePort", targetNamespace = "com.sonic.mquotes.instrument")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@Stateless(mappedName = "MQSecurityServiceRemote")
@Remote(IMQSecurityService.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MQSecurityService implements IMQSecurityService {
	private static MQLogger logger = new MQLogger(MQSecurityService.class, "MQ-WS-SECURITY");
	
	private MQSecurityDAO mqSecurityDAO;
	
	@SuppressWarnings("unused")
	@Resource
	private WebServiceContext context;
	
	@PostConstruct
	public void init() {
		final String methodName = "init()";
		logger.debug(methodName, " --- Successfully invoked init() method.");		
		this.mqSecurityDAO = new MQSecurityDAO();
		logger.debug(methodName, " --- MQSecurityDAO Successfully created.");
	}
	
	@PreDestroy
	public void destroy() {
		final String methodName = "destroy()";
		logger.debug(methodName, " --- Successfully invoked destroy() method.");		
		this.mqSecurityDAO.releaseResources();
		logger.debug(methodName, " --- MQSecurityDAO resources have been successfully released.");
	}
	
	@WebMethod(operationName = "getSecurityDetails")
	public @WebResult(name = "securityDetailsResponse", targetNamespace = "com.sonic.mquotes.instrument")
	MQSecurityDetailsResponse getSecurityDetails(@WebParam(name = "securityDetailsRequest", targetNamespace = "com.sonic.mquotes.instrument") MQSecurityDetailsRequest request) {
		final String methodName = "getSecurityDetails()";
		logger.debug(methodName, " --- Details for security shortName: " + request.getSecurityShortName());		
	    return this.mqSecurityDAO.getSecurityDetails(request);
    }

	@WebMethod(operationName = "getSecurityTransactions")
	public @WebResult(name = "securityTransactionsResponse", targetNamespace = "com.sonic.mquotes.instrument")
	MQSecurityTransactionsResponse getSecurityTransactions(@WebParam(name = "securityTransactionsRequest", targetNamespace = "com.sonic.mquotes.instrument") MQSecurityTransactionsRequest request) {
		final String methodName = "getSecurityTransactions()";
		logger.debug(methodName, " --- Transactions for security shortName: " + request.getSecurityShortName());	   
	    return this.mqSecurityDAO.getSecurityTransactions(request);
    }
}